package com.bosc.modeling.service;

import com.bosc.modeling.dao.gen.ScoreAEntityMapper;
import com.bosc.modeling.dao.gen.ScoreBEntityMapper;
import com.bosc.modeling.pojo.gen.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author yuanyj
 * @version 1.0
 * @date 2019/11/26 10:53
 * @description
 */
@Service
public class ResultService {
    @Autowired
    ScoreAEntityMapper scoreAEntityMapper;
    @Autowired
    ScoreBEntityMapper scoreBEntityMapper;
    @Autowired
    TeamService teamService;
    public int insertScoreA(ScoreAEntity entity){
        return scoreAEntityMapper.insertSelective(entity);
    }
    public List<ScoreAEntity> getHighestScoreA(String competitionId, String teamId){
        ScoreAEntityExample example = new ScoreAEntityExample();
        example.createCriteria().andCompetitionIdEqualTo(competitionId).andTeamIdEqualTo(teamId);
        example.setOrderByClause("score desc");
        return scoreAEntityMapper.selectByExample(example);
    }
    public List<ScoreBEntity> getHighestScoreB(String competitionId, String teamId){
        ScoreBEntityExample example = new ScoreBEntityExample();
        example.createCriteria().andCompetitionIdEqualTo(competitionId).andTeamIdEqualTo(teamId);
        example.setOrderByClause("score desc");
        return scoreBEntityMapper.selectByExample(example);
    }
    public int insertScoreB(ScoreBEntity entity){
        return scoreBEntityMapper.insertSelective(entity);
    }
    public List<ScoreAEntity> getScoreAList(String competitionId,String teamId ,String userId){
        ScoreAEntityExample example = new ScoreAEntityExample();
        example.createCriteria().andCompetitionIdEqualTo(competitionId).andTeamIdEqualTo(teamId).andUserIdEqualTo(userId);
        example.setOrderByClause("time desc");
        return scoreAEntityMapper.selectByExample(example);
    }
    public List<Object> getScoreList(String competitionId,String teamId ,String userId){
        ScoreAEntityExample example = new ScoreAEntityExample();
        example.createCriteria().andCompetitionIdEqualTo(competitionId).andTeamIdEqualTo(teamId).andUserIdEqualTo(userId);
        example.setOrderByClause("time desc");
        List<ScoreAEntity> scoreA = scoreAEntityMapper.selectByExample(example);
        ScoreBEntityExample exampleB = new ScoreBEntityExample();
        example.createCriteria().andCompetitionIdEqualTo(competitionId).andTeamIdEqualTo(teamId).andUserIdEqualTo(userId);
        example.setOrderByClause("time desc");
        List<ScoreBEntity> scoreB = scoreBEntityMapper.selectByExample(exampleB);
        List<Object> list = new ArrayList<>();
        list.addAll(scoreA);
        list.addAll(scoreB);
        return list;
    }

    public List<ScoreAEntity> getSortList(CompetitionEntity competition,short state){
        String competitionId = competition.getCompetitionId();
        List<EnrollEntity> enrolls = teamService.getEnrollByCompetitionId(competitionId);
        List<ScoreAEntity> sort = new ArrayList<>();
        if (state<=1){
            for (EnrollEntity enroll : enrolls){
                List<ScoreAEntity> score_A = getHighestScoreA(competitionId,enroll.getTeamId());
                if (score_A != null && score_A.size() > 0){
                    sort.add(score_A.get(0));
                }
            }
        }else{
            for (EnrollEntity enroll : enrolls){
                List<ScoreAEntity> score_A = getHighestScoreA(competitionId,enroll.getTeamId());
                ScoreAEntity res = null;
                ScoreBEntity res1 = null;
                double score = 0;
                if (score_A != null && score_A.size() > 0){
                    score = score_A.get(0).getScore();
                    res = score_A.get(0);
                }
                List<ScoreBEntity> score_B = getHighestScoreB(competitionId,enroll.getTeamId());
                if (score_B != null && score_B.size() > 0){
                    score = score*competition.getRatioA()+score_B.get(0).getScore()*competition.getRatioB();
                    res1 = score_B.get(0);
                }else {
                    score = score*competition.getRatioA();
                }
                if (res != null || res1 != null){
                    if (res != null){
                        res.setScore(score);
                    }
                    if (res == null){
                        res=new ScoreAEntity();
                        res.setScore(score);
                        res.setTeamName(res1.getTeamName());
                        res.setUserId(res1.getUserId());
                        res.setTeamId(res1.getTeamId());
                        res.setCompetitionId(res1.getCompetitionId());
                        res.setTime(res1.getTime());
                    }
                    sort.add(res);
                }
            }
        }
        Collections.sort(sort);
        return sort;
    }
}
